package com.dcloud.manage.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.dcloud.manage.service.entity.CollectTaskSql;
import com.dcloud.manage.service.ICollectTaskSqlService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 任务sql表
 * @Author: jeecg-boot
 * @Date:   2022-09-07
 * @Version: V1.0
 */
@Api(tags="任务sql表")
@RestController
@RequestMapping("/api/collectTaskSql/v1/")
@Slf4j
public class CollectTaskSqlController extends JeecgController<CollectTaskSql, ICollectTaskSqlService> {
	@Autowired
	private ICollectTaskSqlService collectTaskSqlService;
	
	/**
	 * 分页列表查询
	 *
	 * @param collectTaskSql
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "任务sql表-分页列表查询")
	@ApiOperation(value="任务sql表-分页列表查询", notes="任务sql表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(CollectTaskSql collectTaskSql,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CollectTaskSql> queryWrapper = QueryGenerator.initQueryWrapper(collectTaskSql, req.getParameterMap());
		Page<CollectTaskSql> page = new Page<CollectTaskSql>(pageNo, pageSize);
		IPage<CollectTaskSql> pageList = collectTaskSqlService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param collectTaskSql
	 * @return
	 */
	@AutoLog(value = "任务sql表-添加")
	@ApiOperation(value="任务sql表-添加", notes="任务sql表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody CollectTaskSql collectTaskSql) {
		collectTaskSqlService.save(collectTaskSql);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param collectTaskSql
	 * @return
	 */
	@AutoLog(value = "任务sql表-编辑")
	@ApiOperation(value="任务sql表-编辑", notes="任务sql表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CollectTaskSql collectTaskSql) {
		collectTaskSqlService.updateById(collectTaskSql);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "任务sql表-通过id删除")
	@ApiOperation(value="任务sql表-通过id删除", notes="任务sql表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		collectTaskSqlService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "任务sql表-批量删除")
	@ApiOperation(value="任务sql表-批量删除", notes="任务sql表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.collectTaskSqlService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "任务sql表-通过id查询")
	@ApiOperation(value="任务sql表-通过id查询", notes="任务sql表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		CollectTaskSql collectTaskSql = collectTaskSqlService.getById(id);
		if(collectTaskSql==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(collectTaskSql);
	}

	 /**
	  * 根据任务sqlid即席查询
	  *
	  * @param tid
	  * @return
	  */
	 @AutoLog(value = "根据任务sqlid即席查询")
	 @ApiOperation(value="根据任务sqlid即席查询", notes="根据任务sqlid即席查询")
	 @PostMapping(value = "/jxSelect")
	 public Result<?> jxSelect(@RequestParam(name="tid",required=true) String tid) {
		 List<Map<String,Object>> collectTaskSql = null;
		 try {
			 collectTaskSql = collectTaskSqlService.jxSelect(tid);
		 } catch (ClassNotFoundException e) {
			 throw new RuntimeException(e);
		 } catch (InstantiationException e) {
			 throw new RuntimeException(e);
		 } catch (IllegalAccessException e) {
			 throw new RuntimeException(e);
		 }
		 if(collectTaskSql==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(collectTaskSql);
	 }

}
